<template>
  <div class="container">
    <!--
     |- route
     |- - child.vue
     |- route.vue
     如果是这样的结构必须使用 NuxtPage 标签，否则路由页面无法显示
    -->
    <h3 class="mb-20">路由</h3>
    <!-- 声明式路由导航 -->
    <div class="flex-start c-gap-12">
      <p class="font-s-14">声明式路由导航</p>
      <NuxtLink to="/withinRoute/nesting">
        <el-button>嵌套路由</el-button>
      </NuxtLink>
      <NuxtLink to="/withinRoute/13579">
        <el-button>动态路由(静态传参)</el-button>
      </NuxtLink>
      <NuxtLink
        :to="{
          name: 'withinRoute-id',
          params: {
            id: '24680'
          },
          query: {
            a: 1,
            b: 2
          }
        }">
        <el-button>动态路由(动态传参)</el-button>
      </NuxtLink>
    </div>
    <!-- 编程式路由导航 -->
    <div class="flex-start mt-20">
      <p class="font-s-14 mr-12">编程式路由导航</p>
      <el-button @click="toRoute(0)">嵌套路由</el-button>
      <el-button @click="toRoute(1)">动态路由(静态传参)</el-button>
      <el-button @click="toRoute(2)">动态路由(动态传参)</el-button>
    </div>
    <!-- 路由参数的验证 -->
    <div class="flex-start mt-20">
      <p class="font-s-14 mr-12">路由参数的验证</p>
      <NuxtLink
        :to="{
          path: '/withinRoute/vd',
          query: {
            a: 100,
            b: 200
          }
        }">
        <el-button>参数验证</el-button>
      </NuxtLink>
    </div>
    <!-- 在新的标签页打开 -->
    <div class="flex-start mt-20">
      <p class="font-s-14 mr-12">在新的标签页打开</p>
      <NuxtLink target="_blank" to="/withinRoute/new">
        <el-button>新的标签页</el-button>
      </NuxtLink>
    </div>
    <!-- 路由显示依然是使用 NuxtPage 标签 -->
    <div class="mt-20">
      <NuxtPage></NuxtPage>
    </div>
  </div>
</template>

<script setup>
definePageMeta({
  // 在页面中使用中间件
  // 嵌套路由使用自身 NuxtPage 标签时
  // 在进入其子路由时也会执行中间件
  middleware: 'route'
})

const toRoute = v => {
  switch (v) {
    case 0:
      navigateTo('/withinRoute/nesting')
      break
    case 1:
      navigateTo('/withinRoute/13579')
      break
    case 2:
      navigateTo({
        name: 'withinRoute-id',
        params: {
          id: '24680'
        },
        query: {
          a: 1,
          b: 2
        }
      })
      break
  }
}
</script>

<style lang="scss" scoped></style>
